home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Alles Voor Internet / Tout Pour Internet
/
alles voor internet.iso
/
MacInternet™
/
Telnet
/
NCSA
/
tn3270 2.4d7 source
/
NCSA⁄BYU TCP⁄IP
/
mactools.c.original
< prev
next >
Wrap
Text File
|
1991-06-27
|
11KB
|
438 lines
/*
* mactools.c by Gaige B. Paulsen
*
* adapted from pctools.c by Tim Krauskopf
****************************************************************************
* *
* part of: *
* TCP/UDP/ICMP/IP Network kernel for NCSA Telnet *
* by Tim Krauskopf *
* *
* National Center for Supercomputing Applications *
* 152 Computing Applications Building *
* 605 E. Springfield Ave. *
* Champaign, IL 61820 *
* *
* *
****************************************************************************
*
* those generic tool-type things that only work on Macs.
* includes all hardware-level calls to Ethernet that are unique to the Mac
*
*/
#include <stdio.h>
#include <String.h>
#include <Dialogs.h>
#include <Files.h>
#include <AppleTalk.h>
#include <Events.h>
#include <Memory.h>
#include "protocol.h"
#include "data.h"
#include "fastnet.h"
#include "configrec.h"
#include "ethertalk.h"
#include "atalk.h" /* Dlayer to atalk conversion */
#include "dlayer.h"
#include "ip.h"
#include "ether.h"
#include "elistener.h"
#include "listener.h"
#include "maclook.h"
#ifdef MPW
#define movmem(y,x,z) memcpy(x,y,z)
#endif
/*
* THE ALL-POWERFUL VARIABLE !!!!!!!!!!
*/
int EtherNet
/*
specifies hardware interface to use.
Values are
-99 -- no hardware, using MacTCP (set up in userd.c)
-2 -- FastNet
-1 -- Kinetics Ether SC or Ether SE
0 -- LocalTalk ("built-in")
1 -- 3Com EtherLink/NB (or Apple EtherTalk)
9 .. 14 -- EtherLink/NB in specified NuBus slot
*/
= 0; /* default to LocalTalk */
/**********************************************************************/
/* netarpme
* send an arp to my address. arpinterpret will notice any response.
* Checks for adapters which receive their own broadcast packets.
*/
int netarpme
(
char *s
)
{
if (EtherNet) {
reqarp(s);
}
return(0);
}
/*
* ATdlayersend (ptr, size)
* send size from ptr out the AT port
* All outgoing conversions done here !
*/
ATdlayersend(ptr,size) /*MAC: replaced */
ARPKT *ptr;
unsigned size;
{
char *todata;
AddrBlock *destptr;
int ptype, tsize;
char wds[14], header[18];
MPPParamBlock pb;
static ATARPKT arpbuffer;
int err;
ptype = AIP; /* Default to sending an IP Packet */
pb.DDPsocket=IPSock; /* Sorry.... */
pb.DDPchecksumFlag=FALSE; /* who knows why, but we don't */
pb.DDPwdsPointer=wds; /* Here we go..... */
pb.MPPcsCode=246; /* We want to send a packet */
todata=(char *)ptr + sizeof(DLAYER); /* Point at the real data... */
(uint8 *) destptr = &ptr->d.dest[0]; /* Where is the destination ? */
tsize = size- sizeof(DLAYER); /* Get transmission size */
if (!memcmp( destptr, bseed, 6)) /* Watch for Broadcasts */
movmem(ATbseed, destptr, 4);
if (ptr->d.type == EARP) {
ptype=AARP; /* Arp packet */
arpbuffer.hrd = 3; /* ATalk */
arpbuffer.hln = 4;
arpbuffer.pln = 4;
arpbuffer.pro = ptr->pro;
arpbuffer.op = ptr->op;
movmem( &ptr->sha[0], &arpbuffer.sha[0], 4); /* Move addresses */
movmem( &ptr->spa[0], &arpbuffer.spa[0], 4);
movmem( &ptr->tha[0], &arpbuffer.tha[0], 4);
movmem( &ptr->tpa[0], &arpbuffer.tpa[0], 4);
todata= (char *) &arpbuffer;
todata+= sizeof(ATdlayer); /* New address to go from */
tsize = sizeof(ATARPKT)- sizeof(ATdlayer);
}
/* note incorrect definition of BuildDDPwds in AppleTalk.h:
the destAddress arg is an AddrBlock, not a pointer to one! */
BuildDDPwds( wds, header, todata, *(AddrBlock **) destptr, ptype, tsize );
err=PWriteDDP( &pb, FALSE);
if (err!=0) putln("DDPW Error = %d");
#ifdef SENDNOTICE
else putln("send");
#endif
return(err);
}
/*****************************************************************
* E T H E R N E T / E T H E R T A L K *
*****************************************************************/
struct ETHERWDSstruct {
short len1;
DLAYER *ptr1;
short len2;
DLAYER *ptr2;
short term;
} EWDS;
int ETdlayersend /*MAC: replaced */
(
DLAYER *ptr,
unsigned size
)
{
EtParam ET;
int err;
err = 0;
EWDS.len1=size; /* Could it would it in a WDS? */
EWDS.ptr1=ptr;
EWDS.len2=0;
ET.address = &EWDS;
/* err = */ ETcall( &ET, EWrite);
if (/* err != */ 0)
putln("ETW Error = %d");
#ifdef SENDNOTICE
else putln("send");
#endif
return(err);
}
/**********************************************************************/
/* ETdemux - Ethernet Version of DEMUX routine
*
* returns the number of packets demuxed
*/
int ETdemux
(
int all
)
{
unsigned getcode;
int nmuxed,sizered;
typedef struct peek {
unsigned short count;
unsigned char dest[DADDLEN],
me[DADDLEN];
unsigned short type;
} Dpeek;
Dpeek *firstlook;
nmuxed = 0;
do { /* while all flag is on */
if (bufinfo.bufbig > 0) { /*MAC*/
nmuxed++;
firstlook = (Dpeek *)(bufinfo.bufread); /*MAC where packet is */
getcode = firstlook->type; /* where does it belong? */
switch (getcode) { /* what to do with it? */
case EARP:
case ERARP:
arpinterpret((ARPKT *) firstlook->dest); /* handle [R]ARP packet */
break;
case EIP:
ipinterpret((IPKT *) firstlook->dest); /* handle IP packet */
break;
default:
break;
}
sizered=(firstlook->count+sizeof(Dpeek));
bufinfo.bufbig-=sizered;
#ifdef DEBUGONLY
{
char s[60];
sprintf(s,"buf: %d, pkt: %d",bufinfo.bufbig,sizered);
putln(s);
}
#endif
bufinfo.bufread+=(sizered+1) & 0xFFFE;
if (bufinfo.bufbig<0) bufinfo.bufbig=0;
if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
}
else {
if (ETdeafreset()) {
/* clear deaf flag, buffer overrun */
putln("warning: buffer deaf, restarting");
}
all = 0;
}
} while (all); /* should we look for more to deal with? */
return(nmuxed); /* no packets anymore */
}
/************************************************************************/
/* dlayersend
*
* usage: err = dlayersend(ptr,size)
* err = 0 for successful, non-zero error code otherwise
* ptr is to a dlayer packet header
* size is the number of bytes total
*
* This particular dlayer routine is for Ethernet. It will have to be
* replaced for any other dlayer.
*
* Assumes that dlayer-dependent initializations were made in protinit.c
* to help efficiency.
*
* Ethernet addresses are resolved at higher levels because they will only
* need to be resolved once per logical connection, instead of once per
* packet. Not too layer-like, but hopefully modular.
*
*/
int dlayersend
(
DLAYER *ptr,
int size
)
{
if (EtherNet== 0)
return( ATdlayersend(ptr,size));
if (EtherNet==-2)
return( FNdlayersend(ptr,size));
else
return( ETdlayersend(ptr,size));
}
int ATdemux
(
int all
)
{
unsigned getcode;
int nmuxed,sizered, count;
ATARPKT *firstlook;
static ARPKT arpbuffer;
nmuxed = 0;
do { /* while all flag is on */
if (bufinfo.bufbig > 0) {
nmuxed++;
firstlook = (ATARPKT *)(bufinfo.bufread); /*MAC where packet is */
getcode = firstlook->d.type; /* where does it belong? */
count = firstlook->d.count;
movmem( firstlook->d.dest, arpbuffer.d.dest,4);
movmem( firstlook->d.me, arpbuffer.d.me ,4);
switch (getcode) { /* what to do with it? */
case AARP:
arpbuffer.d.type=EARP; /* Arp packet */
arpbuffer.hrd = 1; /* Enet */
arpbuffer.hln = 6;
arpbuffer.pln = 4;
arpbuffer.pro = firstlook->pro;
arpbuffer.op = firstlook->op;
movmem( &firstlook->sha[0], &arpbuffer.sha[0], 4); /* Move addresses */
movmem( &firstlook->spa[0], &arpbuffer.spa[0], 4);
movmem( &firstlook->tha[0], &arpbuffer.tha[0], 4);
movmem( &firstlook->tpa[0], &arpbuffer.tpa[0], 4);
arpinterpret(&arpbuffer); /* handle ARP packet */
break;
case AIP:
arpbuffer.d.type=EIP;
movmem( &arpbuffer, firstlook, sizeof(DLAYER) );
ipinterpret((IPKT *) firstlook);
break;
default:
break;
}
sizered = count + sizeof(DLAYER);
bufinfo.bufbig-=sizered;
bufinfo.bufread+=(sizered+1) & 0xFFFE;
if (bufinfo.bufbig<0) bufinfo.bufbig=0;
if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
}
else
all = 0;
} while (all); /* should we look for more to deal with? */
return(nmuxed); /* no packets anymore */
}
/**********************************************************************/
/* demux
* find the packets in the buffer, determine their lowest level
* packet type and call the correct interpretation routines
*
* the 'all' parameter tells demux whether it should attempt to empty
* the input packet buffer or return after the first packet is dealt with.
*
* returns the number of packets demuxed
*/
int demux
(
int all
)
{
if (EtherNet==0)
return( ATdemux(all));
if (EtherNet==-2)
return( FNdemux(all));
else
return( ETdemux(all));
}
initbuffer()
{
(Ptr) raw=NewPtr(17000); /* Added by GBP */
if (raw==NULL) return(-1);
bufinfo.bufpt = bufinfo.bufread = bufinfo.buforg
= raw; /* start at the beginning */
bufinfo.bufend = raw+14500; /* leave 2K breathing room, required */
bufinfo.buflim = 12000; /* another 2K breathing room */
bufinfo.bufbig = 0; /* Added by GBP */
if (EtherNet==0)
InitBufPtr(&bufinfo);
else if (EtherNet != -2)
ETInitBufPtr( &bufinfo);
return(0);
}
/***************************************************************************/
/* dlayerinit
* Do machine dependent initializations of whatever hardware we have
* (happens to be 3com ethernet board here )
*/
int dlayerinit
(
void
)
{
if (initbuffer())
return(-1);
if (EtherNet==0)
return(atopen());
if (EtherNet==-2)
return(FNopen());
else
return(ETopen()); /* Pass the Type along */
}
int dlayershut
(
void
)
{
if (EtherNet==0)
return(atclose());
if (EtherNet==-2)
return(FNclose());
else
return(ETclose());
}